const OSS = require('ali-oss')
const R = require('ramda')

const changeToArr = R.unless(R.is(Array), R.of)

let client = new OSS({
  region: 'oss-cn-beijing',
  accessKeyId: 'LTAIa3kNGGWYsB9D',
  accessKeySecret: 'JpOi1RWFevDXctomPgqvQ6q4PmzM2E',
  timeout: 600000
})

client.useBucket('chenjiyuan')

async function put (name, buffer) {
  return await client.put(name, buffer)
}

async function status(path) {
  let result = await client.list({
    prefix: path
  })
  return result.objects || []
}

async function get(path, header) {
  return await client.getStream(path, header)
}

async function getExpiresUrl(path) {
  return await client.signatureUrl(path, {expires: 7200})
}

async function del(path) {
  return await client.deleteMulti(changeToArr(path), {
    quiet: true
  })
}

module.exports = {
  put,
  status,
  get,
  getExpiresUrl,
  del
}
